home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / examples / X_demo_anim.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-09  |  10.8 KB  |  401 lines

  1. /*************** ELECTRONIC VUGRAF ROUTINES
  2. The top part of this file contains general-purpose routines suitable
  3. for creating an animated demonstration of SRGP functionality.
  4. */
  5.  
  6. #include "srgp.h"
  7. #ifdef THINK_C
  8. #include <unix.h>
  9. #include <stdlib.h>
  10. #endif
  11.  
  12. #define EVU_DO(STATEMENT)   EVUprint(#STATEMENT ";"); STATEMENT
  13.  
  14. static attribute_group bund;
  15.  
  16. static int frame_announcement_y, code_reset_y, ycoord, ydelta;
  17.  
  18. static void
  19. EVUdetermineTextInfo (void)
  20. {
  21.    int w, h, d;
  22.    int wd, ht;
  23.  
  24.    SRGP_inquireTextExtent ("hello", &w, &h, &d);
  25.    ydelta = h+d;
  26. #ifndef THINK_C
  27.    ydelta++;
  28. #endif
  29.    SRGP_inquireCanvasSize (0, &wd, &ht);
  30.    frame_announcement_y = ht-h-1;
  31.    code_reset_y = ycoord = ht-ydelta-ydelta;
  32. }
  33.    
  34.    
  35.  
  36. static char buffer[80];
  37.  
  38.  
  39. static void
  40. EVUprint (char *text)
  41. {
  42.    SRGP_inquireAttributes (&bund);
  43.    SRGP_setColor (COLOR_BLACK);
  44.    SRGP_setWriteMode (WRITE_REPLACE);
  45.    SRGP_setClipRectangle (SRGP_defRectangle(0,0,1023,799));
  46.    SRGP_text (SRGP_defPoint(15,ycoord), text);
  47.    ycoord -= ydelta;
  48.    SRGP_setAttributes (&bund);
  49. }
  50.  
  51. static void EVUvertspace(void)
  52. {
  53.    ycoord -= (ydelta>>1);
  54. }
  55.  
  56.  
  57. static void
  58. EVUcomment (char *text)
  59. {
  60.    sprintf (buffer, "/* %s */", text);
  61.    EVUprint (buffer);
  62. }
  63.    
  64.  
  65.  
  66. static void
  67. EVUframe(void)
  68. {
  69.    static int framenumber=0;
  70.    int w, h, d;
  71.  
  72.    framenumber++;
  73.    SRGP_inquireAttributes (&bund);
  74.    SRGP_setClipRectangle (SRGP_defRectangle(0,0,1023,799));
  75.    SRGP_setColor (COLOR_WHITE);
  76.    SRGP_setFillStyle (SOLID);
  77.    SRGP_setWriteMode (WRITE_REPLACE);
  78.    SRGP_inquireTextExtent ("Frame #XXXX ", &w, &h, &d);
  79.    SRGP_fillRectangle 
  80.       (SRGP_defRectangle
  81.      (4, frame_announcement_y-d,
  82.       4+w, frame_announcement_y+h));
  83.    sprintf (buffer, "Frame #%4d    ", framenumber);
  84.    SRGP_setColor (COLOR_BLACK);
  85.    SRGP_text (SRGP_defPoint(4,frame_announcement_y), buffer);
  86.    SRGP_setAttributes (&bund);
  87.  
  88.    EVUvertspace();
  89.  
  90.    SRGP_waitEvent(-1);
  91. }
  92.  
  93. static void
  94. EVUbegin(void)
  95. {
  96.    char inpt[2];
  97.  
  98.  
  99.    SRGP_begin ("SRGP Demonstration", 1024, 800, 3, FALSE);
  100.    SRGP_setKeyboardProcessingMode (RAW);
  101.    SRGP_setInputMode (KEYBOARD, EVENT);
  102.  
  103. #ifdef THINK_C
  104.    SRGP_loadFont (0, "Geneva.9");
  105. #endif
  106.  
  107.    EVUdetermineTextInfo ();
  108.  
  109.    SRGP_text (SRGP_defPoint(10,500), 
  110.           "An Introduction to SRGP");
  111.    SRGP_text (SRGP_defPoint(10,470),
  112.               "Any keystroke yields next frame of the sequence...");
  113.    SRGP_waitEvent (-1);
  114. }
  115.  
  116. static void
  117. EVUclear(void)
  118. {
  119.    SRGP_inquireAttributes (&bund);
  120.    SRGP_setColor (COLOR_WHITE);
  121.    SRGP_setFillStyle (SOLID);
  122.    SRGP_setWriteMode (WRITE_REPLACE);
  123.    SRGP_setClipRectangle (SRGP_defRectangle(0,0,1023,799));
  124.    SRGP_fillRectangleCoord (0,0, 1023,799);
  125.    SRGP_setAttributes (&bund);
  126.    ycoord = code_reset_y;
  127. }
  128.  
  129.  
  130. #ifdef THINK_C
  131. static void usleep (int n)
  132. {
  133.    while (n--);
  134. }
  135. #endif
  136.    
  137.  
  138.  
  139. static void makepatterns(void)
  140. {   
  141.    EVU_DO (SRGP_fillRectangleCoord (560,60, 940,540));
  142.    EVU_DO (SRGP_setFillBitmapPattern (2));
  143.    EVU_DO (SRGP_fillRectangleCoord (580,80, 920,520));
  144.    EVU_DO (SRGP_setFillBitmapPattern (3));
  145.    EVU_DO (SRGP_fillRectangleCoord (600,100, 900,500));
  146.    EVU_DO (SRGP_setFillBitmapPattern (4));
  147.    EVU_DO (SRGP_fillRectangleCoord (620,120, 880,480));
  148.    EVU_DO (SRGP_setFillBitmapPattern (5));
  149.    EVU_DO (SRGP_fillRectangleCoord (640,140, 860,460));
  150.    EVU_DO (SRGP_setFillBitmapPattern (6));
  151.    EVU_DO (SRGP_fillRectangleCoord (660,160, 840,440));
  152.    EVU_DO (SRGP_setFillBitmapPattern (7));
  153.    EVU_DO (SRGP_fillRectangleCoord (680,180, 820,420));
  154.    EVU_DO (SRGP_setFillBitmapPattern (8));
  155.    EVU_DO (SRGP_fillRectangleCoord (700,200, 800,400));
  156. }
  157.  
  158.  
  159. void SRGP__setCanvasDefaults (canvasID);
  160.  
  161.  
  162. main()
  163. {
  164.    point bottomleft, topright, data[3], destination;
  165.    static int xcoords[6]={100,200,300,400,500,600};
  166.    static int ycoords[6]={105,240,270,378,118,20};
  167.    rectangle largerect, cliprect;
  168.    int numofplanes;
  169.    long i;
  170.  
  171.  
  172.    EVUbegin();
  173.    EVUclear();
  174.  
  175.    EVUcomment ("The screen-canvas coordinate system has origin at lower-left");
  176.    EVU_DO (SRGP_lineCoord (0,0, 1023,799));
  177.    EVUframe();
  178.  
  179.    EVUcomment ("Drawing a single pixel");
  180.    EVU_DO (SRGP_pointCoord (300,100));
  181.    EVUframe();
  182.  
  183.    EVUcomment ("Clipping takes care of non-existent pixels");
  184.    EVU_DO (SRGP_lineCoord (-50,-237, 1282,1888));
  185.    EVUframe();
  186.  
  187.    EVUclear();
  188.  
  189.    EVUcomment ("Geometric data structures can be used if desired");
  190.    EVUprint ("point bottomleft, topright;");
  191.    EVUvertspace();
  192.    EVU_DO (bottomleft = SRGP_defPoint (0, 0));
  193.    EVU_DO (topright = SRGP_defPoint (1023, 799));
  194.    EVU_DO (SRGP_line (bottomleft, topright));
  195.    EVUframe();
  196.  
  197.  
  198.    EVUprint ("rectangle largerect;");
  199.    EVUvertspace();
  200.    EVU_DO (largerect = SRGP_defRectangle (750,100, 1000,600));
  201.    EVU_DO (SRGP_rectangle (largerect));
  202.    EVU_DO (SRGP_rectangleCoord (600,25, 800,120));
  203.    EVUframe();
  204.  
  205.    EVUclear();
  206.  
  207.    EVUcomment ("Attributes affect the appearance of primitives");
  208.    EVU_DO (SRGP_setLineStyle (DASHED));
  209.    EVU_DO (SRGP_ellipse (largerect));
  210.    EVUframe();
  211.  
  212.    EVU_DO (SRGP_setLineWidth (10));
  213.    EVU_DO (SRGP_ellipseArc (largerect, 45, 270));
  214.    EVUframe();
  215.  
  216.    EVUcomment ("Pen style affects framed (outline) primitives");
  217.    EVU_DO (SRGP_setPenStyle (BITMAP_PATTERN_OPAQUE));
  218.    EVU_DO (SRGP_setPenBitmapPattern (26));
  219.    EVU_DO (SRGP_ellipseArc (largerect, 270, 45));
  220.    EVUframe();
  221.  
  222.    EVUclear();
  223.  
  224.    EVUcomment ("Fill style affects filled primitives");
  225.    EVU_DO (SRGP_fillRectangle (largerect));
  226.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  227.    EVU_DO (SRGP_setFillBitmapPattern (23));
  228.    EVU_DO (SRGP_fillRectangleCoord (700,25, 800,120));
  229.    EVUframe();
  230.  
  231.    EVU_DO (SRGP_setFillBitmapPattern (6));
  232.    EVU_DO (SRGP_fillEllipse (largerect));
  233.    EVUframe();
  234.  
  235.    EVUclear();
  236.  
  237.    EVUcomment ("Let's reset attributes to normal...");
  238.    EVU_DO (SRGP_setPenStyle (SOLID));
  239.    EVU_DO (SRGP_setFillStyle (SOLID));
  240.    EVU_DO (SRGP_setLineStyle (CONTINUOUS));
  241.    EVU_DO (SRGP_setLineWidth (1));
  242.    EVUvertspace();
  243.    EVUcomment ("Primitives can be generated using data structures");
  244.    EVUprint ("int xcoords[6], ycoords[6];");
  245.    EVUprint ("point data[3];");
  246.    EVUvertspace();
  247.    EVUcomment ("Assume arrays have been initialized");
  248.    EVU_DO (SRGP_polyLineCoord (6, xcoords, ycoords));
  249.    EVUframe();
  250.  
  251.    data[0]=SRGP_defPoint(600,300);
  252.    data[1]=SRGP_defPoint(800,600);
  253.    data[2]=SRGP_defPoint(1000,400);
  254.    EVU_DO (SRGP_polyLine (3, data));
  255.    EVUframe();
  256.  
  257.    EVUprint ("/* polygon = polyline + (line joining 1st and last vertex) */");
  258.    EVU_DO (SRGP_polygon (3, data));
  259.    EVUframe();
  260.  
  261.    EVU_DO (SRGP_fillPolygon (3, data));
  262.    EVUframe();
  263.  
  264.    EVUclear();
  265.  
  266.  
  267.    EVUclear();
  268.  
  269.    EVUcomment ("Default clip rectangle is entire screen");
  270.    EVU_DO (cliprect = SRGP_inquireClipRectangle());
  271.    EVU_DO (SRGP_setLineStyle (DASHED));
  272.    EVU_DO (SRGP_rectangle (cliprect));
  273.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  274.    makepatterns();
  275.    EVUframe();
  276.                 
  277.    EVUclear();
  278.  
  279.    EVUcomment ("Application can control the clip rectangle");
  280.    EVU_DO (cliprect = SRGP_defRectangle (500,0, 850,500));
  281.    EVU_DO (SRGP_setClipRectangle (cliprect));
  282.    EVU_DO (SRGP_rectangle (cliprect));
  283.    EVUframe();
  284.  
  285.    EVUcomment ("The effect of the clip rectangle");
  286.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  287.    makepatterns();
  288.    EVUframe();
  289.    EVUclear();
  290.  
  291.    SRGP__setCanvasDefaults (SCREEN_CANVAS);
  292.  
  293.    EVUcomment ("The write-mode attribute");
  294.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  295.    EVU_DO (SRGP_setFillBitmapPattern (12));
  296.    EVU_DO (SRGP_fillRectangleCoord (0,100, 300,200));
  297.    EVU_DO (SRGP_setFillBitmapPattern (7));
  298.    EVU_DO (SRGP_fillRectangleCoord (100,0, 200,200));
  299.    EVUframe();
  300.  
  301.    EVU_DO (SRGP_setWriteMode (WRITE_OR));
  302.    EVU_DO (SRGP_setFillBitmapPattern (12));
  303.    EVU_DO (SRGP_fillRectangleCoord (400,100, 700,200));
  304.    EVU_DO (SRGP_setFillBitmapPattern (7));
  305.    EVU_DO (SRGP_fillRectangleCoord (500,0, 600,200));
  306.    EVUframe();
  307.  
  308.    EVU_DO (SRGP_setWriteMode (WRITE_XOR));
  309.    EVU_DO (SRGP_setFillBitmapPattern (12));
  310.    EVU_DO (SRGP_fillRectangleCoord (800,100, 1100,200));
  311.    EVU_DO (SRGP_setFillBitmapPattern (7));
  312.    EVU_DO (SRGP_fillRectangleCoord (900,0, 1000,200));
  313.    EVUframe();
  314.  
  315.    EVUcomment ("XOR is used for reversible highlighting");
  316.    EVU_DO (SRGP_setFillStyle (SOLID));
  317.    EVU_DO (SRGP_setColor (COLOR_BLACK));
  318.    EVU_DO (SRGP_fillRectangleCoord (0,0, 150,400));
  319.    EVUframe();
  320.    EVU_DO (SRGP_fillRectangleCoord (0,0, 150,400));
  321.    EVUframe();
  322.  
  323.    EVUclear();
  324.  
  325.    EVUcomment ("The COPY-PIXEL operation");
  326.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  327.    EVU_DO (SRGP_setWriteMode (WRITE_REPLACE));
  328.    makepatterns();
  329.    EVUcomment ("Let's mark the source and destination");
  330.    EVU_DO (SRGP_rectangle (largerect));
  331.    EVU_DO (destination = SRGP_defPoint (480,300));
  332.    EVU_DO (SRGP_marker (destination));
  333.    EVUframe();
  334.  
  335.    EVU_DO (SRGP_copyPixel (SCREEN_CANVAS, largerect, destination));
  336.    EVUframe();
  337.  
  338.    EVUcomment ("This copy goes off the screen");
  339.    EVU_DO (destination = SRGP_defPoint (500,730));
  340.    EVU_DO (SRGP_copyPixel (SCREEN_CANVAS, largerect, destination));
  341.    EVUframe();
  342.  
  343.    EVUcomment ("This copy uses OR write-mode");
  344.    EVU_DO (SRGP_setWriteMode (WRITE_OR));
  345.    EVU_DO (destination = SRGP_defPoint (600,460));
  346.    EVU_DO (SRGP_copyPixel (SCREEN_CANVAS, largerect, destination));
  347.    EVUframe();
  348.  
  349.    EVUclear();
  350.  
  351.    numofplanes = SRGP_inquireCanvasDepth();
  352.    if (numofplanes == 1) {
  353.       EVUprint ("This is the end of the demo for non-color displays.");
  354.       EVUprint ("Goodbye!");
  355.       EVUframe();
  356.       SRGP_end();
  357.       exit(0);
  358.    }
  359.  
  360.    EVUcomment ("Color lookup table is initially set to 0=white, 1=black");
  361.    EVUcomment ("No other entries of table are preset");
  362.    EVU_DO (SRGP_loadCommonColor (2, "LightSeaGreen"));
  363.    EVU_DO (SRGP_loadCommonColor (3, "OrangeRed"));
  364.    EVU_DO (SRGP_setColor (2));
  365.    EVU_DO (SRGP_setWriteMode (WRITE_REPLACE));
  366.    EVU_DO (SRGP_setFillStyle (SOLID));
  367.    EVU_DO (SRGP_fillRectangleCoord (500,100, 800,200));
  368.    EVUframe();
  369.    EVUcomment ("foreground is now green, background still white");
  370.    EVU_DO (SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE));
  371.    EVU_DO (SRGP_setFillBitmapPattern (16));
  372.    EVU_DO (SRGP_fillRectangleCoord (530,130, 830,230));
  373.    EVUframe();
  374.    EVUcomment ("foreground is green, background is now orange-red");
  375.    EVU_DO (SRGP_setBackgroundColor (3));
  376.    EVU_DO (SRGP_fillRectangleCoord (560,160, 860,260));
  377.    EVUframe();
  378.    EVUcomment ("animation via color table modification");
  379.    EVUcomment ("(warning: code actually being run is artificially slowed)");
  380.    EVUprint ("for (i=0; i<30; i++) {");
  381.    EVUprint ("   SRGP_loadCommonColor (0, \"OrangeRed\");");
  382.    EVUprint ("   SRGP_loadCommonColor (0, \"White\");");
  383.    EVUprint ("}");
  384.  
  385.    for (i=0; i<30; i++) {
  386.        SRGP_loadCommonColor (0, "OrangeRed"); SRGP_refresh();
  387.        usleep(60000);
  388.        SRGP_loadCommonColor (0, "White"); SRGP_refresh();
  389.        usleep(60000);
  390.    }
  391.  
  392.    EVUcomment ("Finished!");
  393.    EVUframe();
  394.  
  395.    EVUclear();
  396.    EVUprint ("/********* Bye! ***********/");
  397.    EVUframe();
  398.  
  399.    SRGP_end();
  400. }
  401.